package com.huawei.updatesdk.a.a.a;

import android.content.Context;
import com.huawei.hms.framework.network.restclient.hwhttp.HttpContants;
import com.huawei.updatesdk.a.a.c.c;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.util.zip.GZIPOutputStream;
import javax.net.ssl.HttpsURLConnection;
import org.apache.http.conn.ssl.SSLSocketFactory;

public class b {
    private HttpURLConnection a = null;

    public static class a {
        private String a;
        private int b;

        public String a() {
            return (String) com.huawei.updatesdk.service.e.a.a(this.a);
        }
    }

    private HttpURLConnection a(String str, Context context) throws IOException, CertificateException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException, IllegalAccessException {
        URL url = new URL(str);
        HttpURLConnection httpURLConnection = null;
        Proxy d = com.huawei.updatesdk.a.a.c.b.b.d(context);
        if ("https".equals(url.getProtocol())) {
            httpURLConnection = d == null ? (HttpsURLConnection) url.openConnection() : (HttpsURLConnection) url.openConnection(d);
            ((HttpsURLConnection) httpURLConnection).setSSLSocketFactory(c.a(context));
            ((HttpsURLConnection) httpURLConnection).setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
        }
        return "http".equals(url.getProtocol()) ? (HttpURLConnection) url.openConnection() : httpURLConnection;
    }

    private byte[] a(byte[] bArr) {
        ByteArrayOutputStream byteArrayOutputStream;
        DataOutputStream dataOutputStream;
        Throwable e;
        Throwable th;
        DataOutputStream dataOutputStream2 = null;
        try {
            byteArrayOutputStream = new ByteArrayOutputStream();
            try {
                dataOutputStream = new DataOutputStream(new GZIPOutputStream(byteArrayOutputStream, bArr.length));
            } catch (IOException e2) {
                e = e2;
                try {
                    com.huawei.updatesdk.a.a.b.a.a.a.a("HttpsUtil", "gzip error!", e);
                    if (dataOutputStream2 != null) {
                        try {
                            dataOutputStream2.close();
                        } catch (Throwable e3) {
                            com.huawei.updatesdk.a.a.b.a.a.a.a("HttpsUtil", "gzip error!", e3);
                        }
                    }
                    return byteArrayOutputStream.toByteArray();
                } catch (Throwable th2) {
                    th = th2;
                    if (dataOutputStream2 != null) {
                        try {
                            dataOutputStream2.close();
                        } catch (Throwable e32) {
                            com.huawei.updatesdk.a.a.b.a.a.a.a("HttpsUtil", "gzip error!", e32);
                        }
                    }
                    throw th;
                }
            }
            try {
                dataOutputStream.write(bArr, 0, bArr.length);
                dataOutputStream.flush();
                if (dataOutputStream != null) {
                    try {
                        dataOutputStream.close();
                    } catch (Throwable e322) {
                        com.huawei.updatesdk.a.a.b.a.a.a.a("HttpsUtil", "gzip error!", e322);
                    }
                }
            } catch (IOException e4) {
                e322 = e4;
                dataOutputStream2 = dataOutputStream;
                com.huawei.updatesdk.a.a.b.a.a.a.a("HttpsUtil", "gzip error!", e322);
                if (dataOutputStream2 != null) {
                    dataOutputStream2.close();
                }
                return byteArrayOutputStream.toByteArray();
            } catch (Throwable th3) {
                th = th3;
                dataOutputStream2 = dataOutputStream;
                if (dataOutputStream2 != null) {
                    dataOutputStream2.close();
                }
                throw th;
            }
        } catch (Throwable th4) {
            e322 = th4;
            byteArrayOutputStream = null;
            com.huawei.updatesdk.a.a.b.a.a.a.a("HttpsUtil", "gzip error!", e322);
            if (dataOutputStream2 != null) {
                dataOutputStream2.close();
            }
            return byteArrayOutputStream.toByteArray();
        }
        return byteArrayOutputStream.toByteArray();
    }

    public a a(String str, String str2, String str3, String str4, Context context) throws IOException, CertificateException, IllegalAccessException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
        Throwable th;
        a aVar = new a();
        HttpURLConnection a;
        Closeable dataOutputStream;
        try {
            a = a(str, context);
            if (a == null) {
                if (a != null) {
                    a.disconnect();
                }
                c.a(null);
                c.a(null);
            } else {
                try {
                    this.a = a;
                    a.setDoInput(true);
                    a.setDoOutput(true);
                    a.setUseCaches(false);
                    a.setConnectTimeout(5000);
                    a.setReadTimeout(10000);
                    a.setRequestMethod("POST");
                    a.setRequestProperty(HttpContants.KEY_CONTENT_TYPE, "application/x-gzip");
                    a.setRequestProperty("Content-Encoding", "gzip");
                    a.setRequestProperty("Connection", "Keep-Alive");
                    a.setRequestProperty("User-Agent", str4);
                    dataOutputStream = new DataOutputStream(a.getOutputStream());
                } catch (Throwable th2) {
                    th = th2;
                    dataOutputStream = null;
                    if (a != null) {
                        a.disconnect();
                    }
                    c.a(dataOutputStream);
                    c.a(null);
                    throw th;
                }
                try {
                    Closeable bufferedInputStream;
                    dataOutputStream.write(a(str2.getBytes(str3)));
                    dataOutputStream.flush();
                    int responseCode = a.getResponseCode();
                    aVar.b = responseCode;
                    if (responseCode == 200) {
                        bufferedInputStream = new BufferedInputStream(a.getInputStream());
                    } else {
                        Object bufferedInputStream2 = new BufferedInputStream(a.getErrorStream());
                    }
                    com.huawei.updatesdk.a.a.c.b bVar = new com.huawei.updatesdk.a.a.c.b();
                    byte[] b = a.a().b();
                    while (true) {
                        int read = bufferedInputStream.read(b);
                        if (read == -1) {
                            break;
                        }
                        bVar.a(b, read);
                    }
                    a.a().a(b);
                    aVar.a = bVar.a();
                    if (a != null) {
                        a.disconnect();
                    }
                    c.a(dataOutputStream);
                    c.a(bufferedInputStream);
                } catch (Throwable th3) {
                    th = th3;
                    if (a != null) {
                        a.disconnect();
                    }
                    c.a(dataOutputStream);
                    c.a(null);
                    throw th;
                }
            }
            return aVar;
        } catch (Throwable th4) {
            th = th4;
            a = null;
            dataOutputStream = null;
            if (a != null) {
                a.disconnect();
            }
            c.a(dataOutputStream);
            c.a(null);
            throw th;
        }
    }
}
